﻿using System.IO;
using System.Text;
using NUnit.Framework;
using SharpSvn;
using SvnFlow;
using System.Collections.Generic;

namespace SvnFlowTests
{
    [TestFixture]
    public class ProgramTest
    {
        [Test]
        public void MergeTest()
        {
            var workCopy = Path.GetFullPath("right");

            TestHelper.ReCreateRepositoryFromDump(TestHelper.m_repo1Path, TestHelper.Rep1DumpName);
            TestHelper.ReCreateRepositoryFromDump(TestHelper.m_repo2Path, TestHelper.Rep2DumpName);

            var file = Path.GetFullPath("temp.xml");

            var options = new CmdOptions();

            CreateConfFile(file, null);
            Program.MergeBranches(options, file);
            var changes = GetFolderChanges(workCopy);
            Assert.That(changes, Is.EquivalentTo(
                    new List<string>
                        {
                            string.Format(@"Added None {0}\DiffSub\text3.txt",workCopy),
                            string.Format(@"Added None {0}\DiffSub",workCopy),
                            string.Format(@"Added None {0}\text1.txt",workCopy),
                            string.Format(@"Added None {0}\text2.txt",workCopy),
                            string.Format(@"Added None {0}\text4.txt",workCopy),
                        }
                ));

            TestHelper.DeleteRepository(TestHelper.m_repo1Path);
            TestHelper.DeleteRepository(TestHelper.m_repo2Path);
            SvnHelper.DeleteRecursiveFolder(Path.GetFullPath("left"));
            SvnHelper.DeleteRecursiveFolder(workCopy);
            File.Delete(file);
        }

        [Test]
        public void MergeTest2()
        {
            var workCopy = Path.GetFullPath("left");

            TestHelper.ReCreateRepositoryFromDump(TestHelper.m_repo1Path, TestHelper.Rep1DumpName);
            TestHelper.ReCreateRepositoryFromDump(TestHelper.m_repo2Path, TestHelper.Rep2DumpName);

            var file = Path.GetFullPath("temp.xml");

            var options = new CmdOptions { BackDirection = true };

            CreateConfFile(file, new List<FolderMap> { new FolderMap { Left = "DiffSub", Right = "SubFolder" } });
            Program.MergeBranches(options, file);
            var changes = GetFolderChanges(workCopy);
            Assert.That(changes, Is.EquivalentTo(
                    new List<string>
                        {
                            string.Format(@"Added None {0}\DiffSub\text-6.txt",workCopy),
                        }
                ));

            TestHelper.DeleteRepository(TestHelper.m_repo1Path);
            TestHelper.DeleteRepository(TestHelper.m_repo2Path);
            SvnHelper.DeleteRecursiveFolder(Path.GetFullPath("left"));
            SvnHelper.DeleteRecursiveFolder(workCopy);
            File.Delete(file);
        }

        #region Helper functions
        private static void CreateConfFile(string file, IEnumerable<FolderMap> folders)
        {
            var sb = new StringBuilder();
            sb.AppendLine("<?xml version=\"1.0\"?>");
            sb.AppendLine("<Branches xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" MergeSvnProperties=\"svn:executable,svn:mime-type,svn:ignore,svn:keywords,svn:eol-style,svn:special,svn:needs-lock\">");
            sb.AppendFormat("  <Left Uri=\"file:///{0}\" />\n", TestHelper.m_repo1Path.Replace('\\', '/'));
            sb.AppendFormat("  <Right Uri=\"file:///{0}\" />\n", TestHelper.m_repo2Path.Replace('\\', '/'));
            sb.AppendLine("  <History />");
            if (folders == null)
            {
                sb.AppendLine("  <MappedFolders />");
            }
            else
            {
                sb.AppendLine("  <MappedFolders>");
                foreach (var folderMap in folders)
                {
                    sb.AppendFormat("    <FolderMap Left=\"{0}\" Right=\"{1}\" />\n", folderMap.Left, folderMap.Right);
                }
                sb.AppendLine("  </MappedFolders>");
            }
            sb.Append("</Branches>");
            File.WriteAllText(file, sb.ToString());
        }

        private static List<string> GetFolderChanges(string workPath)
        {
            var sl = new List<string>();
            new SvnClient().Status(
                workPath,
                new SvnStatusArgs { RetrieveRemoteStatus = true },
                (o, args) => sl.Add(args.LocalContentStatus + " " + args.LocalPropertyStatus + " " + args.Path));
            return sl;
        }
        #endregion
    }
}
